package com.wx.springboot.step9back.controller;

import com.wx.springboot.step9back.common.api.CommonResult;
import com.wx.springboot.step9back.model.Logs;
import com.wx.springboot.step9back.model.Page;
import com.wx.springboot.step9back.service.LogsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/logs")
public class LogsController {

    @Autowired
    private LogsService logsService;

    @PreAuthorize("hasAuthority('wx:logs:read')")
    @GetMapping("/getMyLogsList")
    public CommonResult getMyLogsList(@RequestParam Map<String, Object> data){
        HashMap<String, Object> map = new HashMap<>();
        List<Logs> logs = logsService.getMyLogsList(data);
        int count = logsService.getMyLogsCount(data);
        Page page = new Page();
        page.setList(logs);
        page.setTotal(count);
        map.put("list",logs);
        map.put("count",count);
        return CommonResult.success(map);
    }

    @PreAuthorize("hasAuthority('wx:logs:read')")
    @GetMapping("/getSelectList")
    public CommonResult getSelectList(@RequestParam Map<String, Object> data){
        HashMap<String, Object> map = new HashMap<>();
        List<Logs> logs = logsService.getSelectList(data);
        int count = logsService.getSelectCount(data);
        Page page = new Page();
        page.setList(logs);
        page.setTotal(count);
        map.put("list",logs);
        map.put("count",count);
        return CommonResult.success(map);
    }

    @PreAuthorize("hasAuthority('wx:logs:read')")
    @GetMapping("/getLogsById")
    public CommonResult getLogsById(@RequestParam Integer id){
        HashMap<String, Object> map = new HashMap<>();
        Logs logs = logsService.getLogsById(id);
        map.put("data",logs);
        return CommonResult.success(map);
    }

    @PreAuthorize("hasAuthority('wx:logs:create')")
    @PostMapping("/saveLogs")
    public CommonResult saveLogs(@RequestBody Logs logs){
        HashMap<String, String> map = new HashMap<>();
        String result = logsService.saveLogs(logs);
        map.put("data",result);
        return CommonResult.success(map);
    }

    @PreAuthorize("hasAuthority('wx:logs:create')")
    @PostMapping("/submitLogs")
    public CommonResult submitLogs(@RequestBody Logs logs){
        HashMap<String, String> map = new HashMap<>();
        String result = logsService.submitLogs(logs);
        map.put("data",result);
        return CommonResult.success(map);
    }

    @PreAuthorize("hasAuthority('wx:logs:update')")
    @PostMapping("/saveCheckLogs")
    public CommonResult saveCheckLogs(@RequestBody Logs logs){
        HashMap<String, Integer> map = new HashMap<>();
        int result = logsService.saveCheckLogs(logs);
        map.put("data",result);
        return CommonResult.success(map);
    }

    @PreAuthorize("hasAuthority('wx:logs:update')")
    @PostMapping("/submitCheckLogs")
    public CommonResult submitCheckLogs(@RequestBody Logs logs){
        HashMap<String, Integer> map = new HashMap<>();
        int result = logsService.submitCheckLogs(logs);
        map.put("data",result);
        return CommonResult.success(map);
    }

    @PreAuthorize("hasAuthority('wx:logs:update')")
    @PostMapping("/checkLogs")
    public CommonResult checkLogs(@RequestBody Logs logs){
        HashMap<String, Integer> map = new HashMap<>();
        int result = logsService.checkLogs(logs);
        map.put("data",result);
        return CommonResult.success(map);
    }

    @PreAuthorize("hasAuthority('wx:logs:create')")
    @PostMapping("/createWord")
    public CommonResult createWord(@RequestBody Logs logs){
        logsService.createWord(logs);
        return CommonResult.success("ok");
    }

    @PreAuthorize("hasAuthority('wx:logs:delete')")
    @PostMapping("/delLogs")
    public CommonResult delLogs(@RequestBody Logs logs){
        HashMap<String, Integer> map = new HashMap<>();
        int result = logsService.delLogs(logs);
        map.put("result",result);
        return CommonResult.success(map);
    }

    @PreAuthorize("hasAuthority('wx:logs:delete')")
    @PostMapping("/batchDeletion")
    public CommonResult batchDeletion(@RequestParam List<Integer> ids){
        HashMap<String, Object> map = new HashMap<>();
        String result = logsService.batchDeletion(ids);
        map.put("data",result);
        return CommonResult.success(map);
    }

    @PreAuthorize("hasAuthority('wx:logs:create')")
    @PostMapping("/createDeletion")
    public CommonResult createDeletion(@RequestParam List<Integer> ids){
      logsService.createDeletion(ids);
      return CommonResult.success("ok");
    }

}
